﻿using System;
using System.Collections.Generic;
using System.Text;

using aspnet.essential.dal;

namespace aspnet.essential.profile
{
    public class DemoProfileProvider : System.Web.Profile.ProfileProvider
    {
        public override int DeleteInactiveProfiles(
System.Web.Profile.ProfileAuthenticationOption authenticationOption,
DateTime userInactiveSinceDate)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override int DeleteProfiles(string[] usernames)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override int DeleteProfiles(
System.Web.Profile.ProfileInfoCollection profiles)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override System.Web.Profile.ProfileInfoCollection FindInactiveProfilesByUserName
(
System.Web.Profile.ProfileAuthenticationOption authenticationOption,
string usernameToMatch,
DateTime userInactiveSinceDate,
int pageIndex,
int pageSize,
out int totalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override System.Web.Profile.ProfileInfoCollection FindProfilesByUserName(System.Web.Profile.ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override System.Web.Profile.ProfileInfoCollection GetAllInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override System.Web.Profile.ProfileInfoCollection GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override int GetNumberOfInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override string ApplicationName
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(
            System.Configuration.SettingsContext context,
            System.Configuration.SettingsPropertyCollection collection
            )
        {
            // System.Configuration.SettingsContext 传递环境参数
            // 现在的实现有两个参数
            // UserName         当前的用户名
            // IsAuthenticated  是否通过验证
            string username = context["UserName"] as string;
            bool isAuthenticated = (bool)context["IsAuthenticated"];

            // System.Configuration.SettingsPropertyCollection 传递属性集合
            // 属性的定义来自配置文件的properties 元素

            // 获取自定义的属性
            System.Configuration.SettingsProperty qq = collection["QQ"];

            // 现在的方法用于获取用户的属性
DemoUserManager um
    = new DemoUserManager();
            string qqValue = um.GetQQ(username);

            // 创建用户保存属性值的集合
            System.Configuration.SettingsPropertyValueCollection pv
                = new System.Configuration.SettingsPropertyValueCollection();

            // 定义单个的属性值
            System.Configuration.SettingsPropertyValue qqItem
                = new System.Configuration.SettingsPropertyValue(qq);
            qqItem.PropertyValue = qqValue;

            pv.Add(qqItem);

            // 最后，返回已经保存属性值的集合
            return pv;

        }

        public override void SetPropertyValues(
            System.Configuration.SettingsContext context,
            System.Configuration.SettingsPropertyValueCollection collection)
        {
            // 第一个参数与上相同
            // 第二个参数为已经有属性值的集合
            string username = context["UserName"] as string;
            string qqValue = collection["QQ"].PropertyValue as string;
            // 通过数据访问层保存
            DemoUserManager um
                = new DemoUserManager();
            um.SaveQQ(username, qqValue);
        }
    }

}